<?php
/**
 * 商铺模型
 */
 class StoreModel extends Model{
 	public $table = 'store';
 	public $validate = array(
 			array('stname','nonull','商铺名称不能为空',2,3),
			array('kind','maxlen:10','菜式类型',3,3),

			array('cnid','nonull','请选择所在栏目',2,3),
			array('did','nonull','请选择所在区域',2,3),

			array('businessstart','nonull','请输入你营业开始时间',2,3),
			array('businessend','nonull','请输入你营业结束时间',2,3),

			array('storesite','maxlen:46','商铺详细地址不得超过46个字符',2,3),

			array('metro','maxlen:46','地铁地址不得超过46个字符',2,3),


			array('bulletin','maxlen:150','商铺公告字数不能超过150个字符',3,3),

			array('risedelivery','nonull','起送价格不能为空',3,3),
			array('delivery','nonull','配送价不能为空',3,3),
			array('deliverytime','nonull','平均送达的时间不能为空',3,3),

			);
 	// 标签的自动完成
 	// public $auto = array(
 	// 	array('favorable','_favorable','method',2,3),
		// );

 	// public function _favorable( $var){
 	// 	if($var) return implode(',', $var);
 	// }



 	public function add_data(){
 		$this->add();
 	}


 	public function get_data($where=NULL){
 		return $this->where($where)->all();
 	}

 	public function get_one_data($where=null){
 		return $this->where($where)->find();
 	}


 	public function drop_db($where){
 		$this->where($where)->delete();
 		K('Tagstore')->delTag_data($where);
 	}


 	public function alter_data($where){
 		$tid = Q('post.attr',0,'intval');
 		
 		$strtid = implode(',', $tid);

 		$resultData = K('Tag')->get_data('tid IN (' . $strtid . ')');
			$pingying = array();

			foreach ($resultData as $value) {
				$pingying[] = $value['pingying'];

			}
			$strpy = implode(',', $pingying);
			$this->data['favorable'] = $strpy;


 		$this->where($where)->save();
 	}

 	function add_store(){
 		$upload = new Upload($path = 'upload/store');

 		$up = $upload->upload();

 		if(empty($up)){

				//  把类里边返回的错误  赋值给  模型
				$this->error = $upload->error;

				// 终止执行
				return false;
			}

			 // 取当前数组
			$up = current($up);

			//data  是Model  的一个方法。其实就是POST 提交过来的数据
			$this->data['thumb'] = $up['path'];
			$tid = Q('post.attr');

			$strtid = implode(',', $tid);

			$resultData = K('Tag')->get_data('tid IN (' . $strtid . ')');
			$pingying = array();

			foreach ($resultData as $value) {
				$pingying[] = $value['pingying'];

			}
			$strpy = implode(',', $pingying);
			$this->data['favorable'] = $strpy;
			//执行添加
			$stid = $this->add();

			//(本来的属性表)
			//
			




			//标签的添加(外表)
			
			foreach (Q('post.attr') as $v) {
			//article_tag 表的操作   把cid
			$TagData = array(
				'stid'=> $stid,
				'tid' => $v,
				
				);
				K('Tagstore')->add_data($TagData);
			}

			return true;

 	}


 }


?>